<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*会员卡管理*/
class Member extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		if(!in_array(request()->action(),array("export_excel"))){
			parent::_initialize();	
		}
    }
	/*会员卡列表*/
	function index(){
		echo $this->fetch();
	}	
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	//新增
	function add(){
		//本地添加
		$shop_id=sp_get_current_shop_id();
		if(request()->isPost()){
			       $card_no=input("card_no");
			       $count=db("Interface")->where("card_no='$card_no' and shop_id=$shop_id ")->count(1);
				   if($count>0){
				   	   $this->ajax_error("卡号已经存在");
				   }
					$member=array(
					   			"card_no"=>$card_no,
					   			"in_varifycation"=>input("in_varifycation"),
					   			"in_varifycation_type"=>input("in_varifycation_type"),
					   			"fingerprint"=>input("fingerprint"),
								"start_date"=>input("start_date"),
								"end_date"=>input("end_date"),
								"mobile"=>input("mobile"),
								"contract_id"=>input("contract_id"),
								"member_id"=>input("member_id"),
								"name"=>input("name"),
								"surplus_num"=>input("surplus_num"),
								"membership_type"=>input("membership_type"),
								"contract_status"=>input("contract_status"),
								"owner_type"=>input("owner_type"),
								"shop_id"=>$shop_id,
					   );
					if(!empty(input("fingerprint"))&&strlen(input("fingerprint"))>500){//指纹验证
					    $this->_model_down_record_save_update(array("shop_id"=>$shop_id,"card_no"=>$card_no,"type"=>1));
					}
					if(!empty(input("icon1"))&&strlen(input("icon1"))>10){//人脸验证
						  $this->_face_code_insertOrupdate(array(
						  							"card_no"=>$card_no,"face1"=>input("face1"),
						  							"icon1"=>input("icon1"),
						  							"shop_id"=>$shop_id,
						  							"name"=>$member["name"],
						  							"mobile"=>$member["mobile"]
						  							));
					}else if(input("in_varifycation_type")==3){//前台验证
					}	
				
			action('OptLog/api_add',['member'=>$member,'icon'=>input("icon1")]);	   
			db("Interface")->insert($member);
			return $this->ajax_success("成功");
		}else{
			echo $this->fetch();die;
	}
}
  	/**
	 * 修改会员
	 */
	function edit(){
		$shop_id=sp_get_current_shop_id();
		if(request()->isPost()){
		   $model=db("Interface")->find(input("id"));
		   $old_model=$model;
		   $old_icon=db("FaceCode")->where("shop_id=$shop_id and card_no='".$model['card_no']."'")->value('icon1');
		   $model["in_varifycation"]=input("in_varifycation");
		   $model["in_varifycation_type"]=input("in_varifycation_type");
		   $model["fingerprint"]=input("fingerprint");
		   
			if(!empty(input("fingerprint"))&&strlen(input("fingerprint"))>500){//指纹验证
				$this->_model_down_record_save_update(array("shop_id"=>$shop_id,"card_no"=>$model['card_no'],"type"=>1));
			}
			if(!empty(input("icon1"))&&strlen(input("icon1"))>10){//人脸验证
				 $this->_face_code_insertOrupdate(array(
						  							"card_no"=>$model['card_no'],"face1"=>input("face1"),
						  							"icon1"=>input("icon1"),"shop_id"=>$shop_id
						  							,"name"=>$model["name"],"mobile"=>$model["mobile"]
						  							));												
			}else{
				db("FaceCode")->where("shop_id=$shop_id and card_no='".$model['card_no']."'")->delete();
			}	
			action('OptLog/api_edit',['member'=>$old_model,'icon'=>empty($old_icon)?"":$old_icon,'member1'=>$model,'icon1'=>input('icon1','')]);	  
			db("Interface")->update($model);
		    return $this->ajax_success("成功");
		}else{
			$model=db("Interface")->find(input("id"));
			$shop_id=sp_get_current_shop_id();
			$this->assign("model",$model);
			$facecode=db("FaceCode")->where(" shop_id=$shop_id and  card_no='".$model['card_no']."'")->find();
			if(empty($facecode)){
				$this->assign("is_bind_face",0);
				$facecode=array("face1"=>null,"icon1"=>null);
			}else{
				$this->assign("is_bind_face",1);
			}
			if(empty($model['fingerprint'])){
				$this->assign("is_bind_finger",0);
			}else{
				$this->assign("is_bind_finger",1);
			}
			
			$this->assign("facecode",$facecode);
			$this->assign("icons",$this->getIcons(explode(",", $facecode['icon1'])));
			$this->assign("borrow_card_no",input("borrow_card_no",""));
			
 			$api_version=db("shop")->where("id=$shop_id")->value("api_version");
			echo $this->fetch();die;
		}
	}
	///制匹配卡
	function fetchcard(){
		$id=input("id");
		$this->assign("card_no",db("Interface")->where("id=$id")->value("card_no"));
		echo $this->fetch();
	}
	
	////查询卡
	function find_bycard(){
		$card_no=input("card_no");
		$shop_id=sp_get_current_shop_id();
		$model=db("interface")->where("card_no='$card_no' and shop_id=$shop_id")->find();
		if(empty($model)){
			return $this->ajax_error("抱歉！无法查找到会员。");
		}
		$this->ajax_success("成功",$model);
	}
	//查询
	function query(){
	    $card_no=input("card_no");
		$shop_id=sp_get_current_shop_id();
		$model=db("interface")->where("card_no='$card_no' and shop_id=$shop_id")->find();
		if(empty($model)){
			$this->ajax_error("无效的卡号");
		}else{
			if($model["owner_type"]==1&&$model["in_varifycation"]==1&&$model["in_varifycation_type"]==3){
				$count=db("VarifyRecord")->where("card_no='$card_no' and shop_id=$shop_id and is_in=0 ")->count(1);
				if($count>0){
					$this->ajax_error("此卡已经验证，请勿重复验证");
				}
			}else{
				$this->ajax_error("此卡不需要前台验证");	
			}
		}
		$headimage=db("InterfaceExtral")->where("card_no='".$model['card_no']."' and shop_id=".$model['shop_id'])->value("head_image");
		if(empty($headimage)){
			$model['headimage']="";
		}else{
			$model['headimage']=$headimage;
		}
		$this->ajax_success("成功",$model);	
	}
	
	/*前台验卡*/
	function varify(){
		$shop_id=sp_get_current_shop_id();
		$this->assign("shop_id",$shop_id);
		if(request()->isPost()){
			$card_no=input("card_no");
			$model=db("interface")->where("card_no='$card_no' and shop_id=$shop_id")->find();
			if(empty($model)){
				$this->ajax_error("无效的卡号");
			}else{
				if($model["owner_type"]==1&&$model["in_varifycation"]==1&&$model["in_varifycation_type"]==3){
					$count=db("VarifyRecord")->where("card_no='$card_no' and shop_id=$shop_id and is_in=0 ")->count(1);
					if($count>0){
						$this->ajax_error("此卡已经验证，请勿重复验证");
					}else{
						db("VarifyRecord")->insert(array("card_no"=>$card_no,"shop_id"=>$shop_id,"is_in"=>0));
						$this->ajax_success("成功");
					}
				}else{
					$this->ajax_error("此卡不需要前台验证");	
				}
			}
		}else{
			echo $this->fetch();
		}
	}
	//同步会员数据
	function sync(){
		if(request()->isPost()){
			$member=array(
					   			"id"=>input("id"),
								"start_date"=>input("start_date"),
								"end_date"=>input("end_date"),
								"mobile"=>input("mobile"),
								"contract_id"=>input("contract_id"),
								"member_id"=>input("member_id"),
								"name"=>input("name"),
								"surplus_num"=>input("surplus_num"),
								"membership_type"=>input("membership_type"),
								"contract_status"=>input("contract_status"),
			);
			db("Interface")->update($member);
			$this->ajax_success($member);
		}else{
			$model=$this->assign("model",db("Interface")->find(input("id")));
			echo $this->fetch();
		}
	}
	/**
	 * 远程获取会员信息
	 * http://smartgym.com/admin/Member/get_member_info/card_no/1354389815/shop_id/4
	 * */
	function get_member_info($card_no){
		$shop_id=sp_get_current_shop_id();
		$mobile=input("mobile","0");
		$result=action("api/OcmApi/GetContractInfo",[$card_no,$shop_id,$mobile]);
		if($result['flag']){
			$this->ajax_success("成功",$result['data']);
		}else{
			$this->ajax_error($result['msg']);
		}
	}

	//根据url 获取特征码		
	function get_face_code(){
		$card_no=input("card_no");
		$member_url=input("member_url");
		$shop_id=sp_get_current_shop_id();
		if(empty($card_no)){
			$this->ajax_error("卡号错误");
		}
		if(empty($member_url)){
			$this->ajax_error("接口中不包括会员头像数据");
		}
		$face1=action("api/JGGApi/ConverUrlImage",['url'=>$member_url,'shop_id'=>$shop_id]);
		$this->ajax_success("成功",array("icon1"=>$member_url,"face1"=>$face1));
	}
	
    /*会员借卡*/
	function  borrow(){
		$shop_id=sp_get_current_shop_id();
		if(request()->isPost()){
			$card_no=input("card_no");//借的卡号
			$count=db("Interface")->where("card_no='$card_no' and shop_id=$shop_id ")->count(1);
			if($count>0){
				return $this->ajax_error("此卡号已经存在");
			}
			//新增一条借记卡
			db("Interface")->insert(array("card_no"=>input("card_no"),"shop_id"=>$shop_id,"name"=>input("name"),"mobile"=>input("mobile"),"is_original"=>0,"owner_type"=>1,"descs"=>input("descs"),"original_id"=>input("id")));
			//新增一条记录
			db("BorrowCard")->insert(array("card_no"=>input("card_no"),"shop_id"=>$shop_id,"name"=>input("name"),"mobile"=>input("mobile"),"descs"=>input("descs"),"original_card_no"=>input("original_card_no"),"owner_type"=>1));
			return $this->ajax_success("成功");
		}else{
			$id=input('id');
			$count=db("Interface")->where("original_id=$id and shop_id=$shop_id and owner_type in(1,2)")->count(1);
			if($count>0){// 存在借卡
			    echo "此会员已经借卡一张，请先归还";	
			    die;
			}
			$model=db("Interface")->find($id);
			$this->assign("model",$model);
			echo $this->fetch();	
		}
	}
	
	/*会员还卡*/
	function  back(){
		$shop_id=sp_get_current_shop_id();
		if(request()->isPost()){
			$card_no=input("card_no");//借的卡号
			//修改借记状态
			db("BorrowCard")->where("card_no='$card_no' and is_back=0 and shop_id=$shop_id")->update(array("back_time"=>date("Y-m-d H:i:s"),"is_back"=>1));
			//删除借记卡
			db("Interface")->where("card_no='$card_no' and shop_id=$shop_id")->delete();
			return $this->ajax_success("成功");
		}else{
			$id=input('id');
			//借记的卡号
			$borrow_card_no=db("Interface")->where("original_id=$id and shop_id=$shop_id")->value("card_no");
			if(empty($borrow_card_no)){
			    echo "此会员没有借卡，无须还卡";	
			    die;
			}
			$this->assign("borrow_card_no",$borrow_card_no);
			$model=db("Interface")->find($id);
			$this->assign("model",$model);
			echo $this->fetch();	
		}
	}
	/*权限*/
	function auth(){
		$shop_id=sp_get_current_shop_id();
		if(request()->isPost()){
			$id=input("id");
			if(!isset($_POST['door_ids'])||empty($_POST['door_ids'])){
				$door_ids=[0];
			}else{
				$door_ids=$_POST['door_ids'];	
			}
			$door_ids=implode(",", $door_ids);
			$list=db("SlidingDoor")->where("id in($door_ids) and shop_id=$shop_id ")->column("code");
			db("Interface")->where("id=$id")->update(array("doors_auth"=>$door_ids));
			$this->ajax_success("成功");
		}else{
			$model=db("Interface")->find(input("id"));
			$this->assign("model",$model);
			$this->assign("doors_auth",$model['doors_auth']);
			$this->assign("doors",db("SlidingDoor")->where("shop_id=$shop_id")->select());
			echo $this->fetch(); die;
		}
	}
	
	//删除卡
	function delete(){
			$id=input("id/d");
			$shop_id=sp_get_current_shop_id();
			$model=db("Interface")->find($id);
			$card_no=$model['card_no'];
			$old_icon=db("FaceCode")->where("shop_id=$shop_id and card_no='$card_no'")->value('icon1');
			$count=db("BorrowCard")->where("shop_id=$shop_id  and is_back=0 and original_card_no=(select card_no from m_interface where id=$id and shop_id=$shop_id)")->count(1);
			if($count>0){
			  return	$this->ajax_error("请先还卡");
			}
			$count=db("Interface")->where("shop_id=$shop_id and owner_type=5 and original_id=$id")->count(1);
			if($count>0){
			  return	$this->ajax_error("此卡存在副卡！请先删除副卡");
			}
			action('OptLog/api_del',['member'=>$model,'icon'=>empty($old_icon)?'':$old_icon]);
			db("FaceCode")->where("shop_id=$shop_id and card_no='$card_no'")->delete();
			db("InterfaceExtral")->where("shop_id=$shop_id and card_no='$card_no'")->delete();
			db("Interface")->where("id=$id and shop_id=$shop_id")->delete();
			
			return $this->ajax_success("成功");
	}
	/**
	 * 提交会员数据
	 * */
	function member_sync_post(){
		//原店铺id
		$origin_shop_id=input("shop_member");
		$card_no=input("card_no");
		//当前店铺id
		$shop_id=sp_get_current_shop_id();
		// 判断该卡是否在本店使用
		$count=db("Interface")->where("card_no='$card_no' and shop_id=$shop_id")->count();
		if($count>0){
			return $this->ajax_error("该卡已经在本门店被使用！");
		}
		
		//同步会员数据
		$interface=db("Interface")->where("card_no='$card_no' and shop_id=$origin_shop_id")->find();
		unset($interface['id']);
		unset($interface['create_time']);
		unset($interface['update_time']);
		$interface['shop_id']=$shop_id;
		if($interface['in_varifycation']==1&&$interface['in_varifycation_type']==2){
			//同步人脸数据
			$facecode=db("FaceCode")->where("card_no='$card_no' and shop_id=$origin_shop_id")->find();
			if(!empty($facecode)){
				unset($facecode['id']);
				unset($facecode['create_time']);
				unset($facecode['update_time']);
				$facecode['shop_id']=$shop_id;
				db("FaceCode")->insert($facecode);	
			}else{
				return $this->ajax_error("人脸验证模板不存在！");
			}
		}
		db("Interface")->insert($interface);
		$this->ajax_success("成功");
	}
	/**
	 * 设置头像
	 */
	function headimage(){
			echo $this->fetch();die;
	}
	/**
	 * 通店卡同步
	 *  http://anke.com/admin/Member/commonshop/shop_id/10/card_no/2238082314
	 * */
	function commonshop(){
		echo  $this->fetch();die;
	}
	/**
	 * 通店卡服务器拉取会员数据并存放
	 */
	 function add_common_member($card_no){
		$shop_id=sp_get_current_shop_id();
		$count=db("Interface")->where("card_no='$card_no' and shop_id=$shop_id")->count();
		if($count>0){
			return $this->ajax_error("该卡已经在本门店被使用！");
		}
		$parent_id=db("Shop")->where("id=$shop_id")->value("parent_id");
		if(empty($parent_id)){
			return $this->ajax_error("店铺品牌不存在(父id 不存在) ");
		}
		
	    $list=db("Interface")->where("card_no='$card_no' and shop_id in(select id from m_shop where parent_id=$parent_id and shop_id!=$shop_id)")->column("shop_id,name,in_varifycation_type");
		if(empty($list)){
			return $this->ajax_error("卡号在其他门店不存在");
		}else{
			$shop_ids=array_keys($list);
			$shop_names=db("Shop")->where("id in(".implode(",", $shop_ids).")")->column("id,shop_name");
			foreach ($list as $key => $value) {
				$list[$key]['shop_name']=$shop_names[$value['shop_id']];
			}
			return $this->ajax_success("",$list);
		}
	}
	/**
	 * 导出excle
	 * */
	public function export_excel(){
		if(empty(input("pageSize"))){
			var_dump("");die;
		}
		$titles_des=array("卡号","姓名","手机","合同号","会员号","有效期开始","有效期结束","状态");
		$titles_field=array("card_no","name","mobile","contract_id","member_id","start_date","end_date","contract_status");
		$data=$this->_ajax_index();
		$list=$data['pageData'];
		foreach ($list as $key => $value) {
			if($value['contract_status']==1){
				$list[$key]['contract_status']="有效";
			}else if($value['contract_status']==2){
				$list[$key]['contract_status']="欠款";
			}else{
				$list[$key]['contract_status']="无效";
			}
		}
		$title="会员信息";
		var_dump(export_excel($titles_des,$titles_field,$list,$title));die;
		die;
	}
	/**
	 * 批量更新合同
	 * */
	function update_contract(){
		if(request()->isPost()){
			
		}else{
			$shop_id=sp_get_current_shop_id();
			$count=db("Interface")->where("owner_type=1 and shop_id=$shop_id")->count(1);
			if($count<1000){
				$this->assign("block",array("0,1000"));
			}else if($count<2000){
				$this->assign("block",array("0,1000","1000,2000"));
			}else if($count<3000){
				$this->assign("block",array("0,1000","1000,2000","2000,3000"));
			}else if($count<4000){
				$this->assign("block",array("0,1000","1000,2000","2000,3000","3000,4000"));
			}else if($count<5000){
				$this->assign("block",array("0,1000","1000,2000","2000,3000","3000,4000","4000,5000"));
			}else if($count<6000){
				$this->assign("block",array("0,1000","1000,2000","2000,3000","3000,4000","4000,5000","5000,6000"));
			}else if($count<7000){
				$this->assign("block",array("0,1000","1000,2000","2000,3000","3000,4000","4000,5000","5000,6000","6000,7000"));
			}else if($count<8000){
				$this->assign("block",array("0,1000","1000,2000","2000,3000","3000,4000","4000,5000","5000,6000","6000,7000","7000,8000"));
			}else if($count<9000){
				$this->assign("block",array("0,1000","1000,2000","2000,3000","3000,4000","4000,5000","5000,6000","6000,7000","7000,8000","8000,9000"));
			}else if($count<10000){
				$this->assign("block",array("0,1000","1000,2000","2000,3000","3000,4000","4000,5000","5000,6000","6000,7000","7000,8000","8000,9000","9000,10000"));
			}
			$this->assign("count",$count);
			echo $this->fetch();
		}
	}
	/**
	 * 同步所有
	 * */
	function sync_all(){
		$blocks=explode(",", input("blocks"));
		$shop_id=sp_get_current_shop_id();
		if(empty($blocks)){
			$this->ajax_error("参数错误");
		}
		$list=db("Interface")->where("owner_type=1 and shop_id=$shop_id")->limit($blocks[0],1000)->field("id,card_no,name,mobile")->select();
		$i=0;
		$k=0;
		$msg_result="";
		foreach ($list as $key => $value) {
			$result=action("api/OcmApi/GetContractInfo",[$value['card_no'],$shop_id,"0"]);
			if($result['flag']){
				$i=$i+1;
				$data=$result['data'];
				$member=array(
								"start_date"=>$data["StartDate"],
								"end_date"=>$data["EndDate"],
								"mobile"=>$data["Mobile"],
								"contract_id"=>$data["ContractID"],
								"member_id"=>$data["MemberID"],
								"name"=>$data["MemberName"],
								"surplus_num"=>$data["SurplusNum"],
								"membership_type"=>$data["MembershipType"],
								"contract_status"=>$data["ContractStatus"],
				);
				db("Interface")->where("id=".$value['id'])->update($member);
			}else{
				$k=$k+1;
				$msg_result.="序号：".$k."&nbsp;&nbsp;&nbsp;，卡号:".$value['card_no'].'&nbsp;&nbsp;&nbsp;，姓名:'.$value['name']."&nbsp;&nbsp;&nbsp;，电话:".$value['mobile'].'</br>';
			}
		}
		$this->ajax_success('成功',htmlspecialchars_decode('<h6>成功更新条数：'.$i.'</h6><h6>失败记录('.(count($list)-$i).'条)如下：</h6>'.$msg_result));
	}
	 /**
	  * 
	  *测试添加会员
	  * 
	  * */
//	 function test_add(){
//	 	$model=db("Interface")->find(47);
//		$facecode=db("FaceCode")->find(12);
//		unset($model['id']);
//		unset($facecode['id']);
//		$data=$model;
//		$data1=$facecode;
//		$array="";
//		$array1="";
//		for($i=80000;$i<100000;$i++){
//			$data['card_no']=$model['card_no']-$i;
//			$data['name']="测试"."_".$i;
//			$data['contract_id']=$model['card_no']-$i;
//			$data['member_id']=$model['card_no']-$i;
//			
//			$array[]=$data;
//			
//			$data1['card_no']=$model['card_no']-$i;
//			$array1[] = $data1;
//			if(count($array)==300){
//				db("Interface")->insertAll($array);
//				db("FaceCode")->insertAll($array1);
//				$array="";
//				$array1="";
//			}
//		}
//		var_dump("cheng");die;
//	 }
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041003");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
					 'card_no'=>array("field" =>"card_no","operator" =>"="),
					 'name'=>array("field" =>"name","operator" =>"="),
					 'mobile'=>array("field" =>"mobile","operator" =>"="),
					 );
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and owner_type=1 and is_original=1 ";
		
		$shop_id=sp_get_current_shop_id();
		$shop_where_and="";
		if(!empty($shop_id)){
			$where_and.=" and shop_id=$shop_id";
			$shop_where_and=" and shop_id=$shop_id";
		}else{
			$where_and.=" and shop_id=0";
			$shop_where_and=" and shop_id=0";
		}
		
		//借计卡条件
		if(!empty(input("borrow_card_no"))){
			$original_id=db("Interface")->where(" is_original=0 $shop_where_and  and owner_type=1 and card_no='".input("borrow_card_no")."'")->value("original_id");
			if(empty($original_id)){
				$original_id=0;
			}
			$where_and.=" and id=".$original_id;
		}
		
		$total=db("interface")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("interface")->field("fingerprint",true)->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		
		$borrow_list=$this->borrow_list();
		foreach ($list as $key => $value) {
			if(isset($borrow_list[$value['id']])){
				$list[$key]["borrow_card_no"]=$borrow_list[$value['id']];
			}else{
				$list[$key]["borrow_card_no"]="";
			}
		}
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
	//插入或者修改facecode
	private function _face_code_insertOrupdate($data){
		if(empty($data)){
			return;
		}else{
			$card_no=$data["card_no"];
			$shop_id=$data['shop_id'];
			$model=db("FaceCode")->where(" card_no='$card_no' and shop_id=$shop_id")->find();
			if(empty($model)){//新增
				db("FaceCode")->insert(array('card_no'=>$data['card_no'],'face1'=>$data['face1'],'icon1'=>$data['icon1'],'shop_id'=>$data['shop_id']));
			}else{//修改
				db("FaceCode")->where("card_no='$card_no' and shop_id='$shop_id'")->update(array("face1"=>$data['face1'],"icon1"=>$data['icon1']));
			}
			$data['type']=2;
			$data['update_time']=date("Y-m-d H:i:s");
			$this->_model_down_record_save_update($data);
			return;
		}
	}
	/**
	 * 模板是否新增或者修改---做记录
	 * */
	private function _model_down_record_save_update($data){
		$shop_id=$data['shop_id'];
		$card_no=$data['card_no'];
		$type=$data['type'];
		$model=db("ModelDownRecord")->where("card_no='$card_no' and shop_id=$shop_id and type=$type ")->field("id,card_no")->find();
		if(empty($model)){
			db("ModelDownRecord")->insert(array("card_no"=>$card_no,"shop_id"=>$shop_id,"type"=>$type,"is_down"=>0));
		}else{
			db("ModelDownRecord")->where("card_no='$card_no' and shop_id=$shop_id and type=$type")->update(array("is_down"=>0,"update_time"=>date("Y-m-d H:i:s")));
			$borrow_card_no=db("Interface")->where(" original_id=(select id from m_interface where card_no='$card_no' and shop_id=$shop_id) and shop_id=$shop_id")->value("card_no");
			if(!empty($borrow_card_no)){
				db("ModelDownRecord")->where("card_no='$borrow_card_no' and shop_id=$shop_id and type=$type")->update(array("is_down"=>0,"update_time"=>date("Y-m-d H:i:s")));
			}
			if($data['type']==2){
				$admin=session("Admin");
				db("FaceCodeRecord")->insert(array('shop_id'=>$data['shop_id'],'card_no'=>$data['card_no'],'name'=>$data['name'],'mobile'=>$data['mobile'],'admin_account'=>$admin['account'],'admin_name'=>$admin['name'],'admin_mobile'=>$admin['mobile']));	
			}
		}
	}
	//获取会员借卡列表
	public function borrow_list(){
		$shop_id=sp_get_current_shop_id();
		if(empty($shop_id)){
			$shop_id=0;
		}
		if(!empty($shop_id)){
			return db("Interface")->where(" shop_id=$shop_id and owner_type=1 and is_original=0")->column("original_id,card_no");	
		}
	}
	//icon1 转arr
	public function getIcons($icon1){
		$icons=["/data/finger/a8.jpg","/data/finger/a8.jpg","/data/finger/a8.jpg"];
		foreach ($icon1 as $key => $value) {
		    if (!empty($value)) {
                $icons[$key]=$value;
            }
		}
		return $icons; 
	}
	/**
	 * 查询会员
	 * */
	public function SearchMember(){
		$shop_id=sp_get_current_shop_id();
		$q=input("q","xxx");
		$list=db("Interface")->fetchsql(FALSE)->where("shop_id=$shop_id and (name like '%$q%' or mobile like '%$q%' or card_no like '%$q%')")->field("card_no as id,CONCAT(name,'--',mobile,\"--\",card_no) as text")->select();
		$sign_card_nos=db("Sign")->where("shop_id=$shop_id and sign_date='".date('Y-m-d')."'")->column("card_no");
		foreach ($list as $key => $value) {
			if(in_array($value['id'], $sign_card_nos)){
				$list[$key]['text']=$value['text']."--(已签到)";
			}
		}
		header("Content-type:application/json;charset=utf-8");
		exit(json_encode($list,TRUE));
	}
}
